This page last changed on May 18, 2007 by aaime.

I wanted to reload the Geoserver catalog without having to go through the web interface to fill out the form fields and click the buttons.
I managed to reload the catalog by using the LWP module in Perl, whenever new layer is added to Geoserver's data collection. If you have Perl, the following script should work for you also:

reload_geoserver_catalog.pl
#!/usr/bin/perl

use HTTP::Request::Common;
use HTTP::Cookies;
use LWP 5.64;
use strict;

reload_geoserver_catalog();

sub reload_geoserver_catalog {
        my $browser = LWP::UserAgent->new;
        my $cookie_jar = HTTP::Cookies->new;

        #Perform geoserver authentication:
        my $url =
        'http://localhost:8080/geoserver/admin/loginSubmit.do';
        my $response = $browser->request(POST $url,
                ['username' => 'someusername', 'password' => 'somepassword', 'submit' => 'Submit']);

        die "Error: ", $response->header('WWW-Authenticate') ||
        'Error accessing',
        # ('WWW-Authenticate' is the realm-name)
        "\n ", $response->status_line, "\n at $url\n Aborting"
        unless $response->is_success;

        $cookie_jar->extract_cookies($response);

        #Load the new catalog:
        $url =
        'http://localhost:8080/geoserver/admin/loadFromXML.do';
        my $req = new HTTP::Request POST => $url;
        $cookie_jar->add_cookie_header($req);
        $response = $browser->request($req);

        #Finally, logout:
        $url =
        'http://localhost:8080/geoserver/admin/logout.do';
        $req = new HTTP::Request POST => $url;
        $cookie_jar->add_cookie_header($req);
        $response = $browser->request($req);
}

If you run Linux, you could also use the curl command, which in my understanding can accomplish the same goal. Here is a link which describes how to use curl.

And here's how to do it in Java, from Sven Tschirner (be sure to adjust your addresses/ username/password):

reload_geoserver_catalog.java
// do login
       URL url=new URL("http://localhost:8080/geoserver/admin/loginSubmit.do?username=goeserverUser&password=geoserverPassword&submit=Submit");
       URLConnection conn = url.openConnection();
       InputStream inStream = conn.getInputStream();
       String responseString=new String();
       BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
       while(in.ready())
       {   responseString+= in.readLine();  }
       System.out.println("------------------------------------------------------\nresponseString:"+responseString);

        // reload
       String cookie=conn.getHeaderField("Set-Cookie");
       System.out.println("cookie-text:"+cookie);
       cookie = cookie.substring(0, cookie.indexOf(";"));
       String cookieName = cookie.substring(0, cookie.indexOf("="));
       String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
       String cookieString=cookieName+"="+cookieValue;
       URL url2=new URL("http://localhost:8080/geoserver/admin/loadFromXML.do");
       URLConnection conn2 = url2.openConnection();
       conn2.setRequestProperty("Cookie",cookieString);                 // set the Cookie for request
       conn2.connect();
       inStream = conn2.getInputStream();
       in = new BufferedReader(new InputStreamReader(inStream));
       responseString=new String();
       while(in.ready())
       {   responseString+= in.readLine();  }
       System.out.println("------------------------------------------------------\nresponseString:"+responseString);

        //logout
       URL url3=new URL("http://localhost:8080/geoserver/admin/logout.do");
       URLConnection conn3 = url3.openConnection();
       conn3.setRequestProperty("Cookie",cookieString);
       conn3.connect();
        inStream = conn3.getInputStream();
       in = new BufferedReader(new InputStreamReader(inStream));
       responseString=new String();
       while(in.ready())
       {   responseString+= in.readLine();  }
      System.out.println("------------------------------------------------------\nresponseString:"+responseString);

The next script for PHP register a new FeatureType and then reload the catalog, from Javier de la Torre (be sure to adjust your addresses/ username/password):
You will need the HttpClient.class.php from Incutio (http://scripts.incutio.com/httpclient/index.php).
This piece of software is just a part of a bigger one that accept some XML, create a temporary table in PotGIS, insert the data and finally register it as a new layer in Geoserver. If you are insterested in such a thing contact me ([email protected])

register_featureType.php
<?php

//Register the new data type using geoserver configuration tool
require_once('HttpClient.class.php');

//Generate a ramdom featureType name (layer)
srand(time());
$layerName = (rand()%99999999);

//Datastore from where the FeatureType will be created (database in PostGIS)
$datastore="localhost_postgis";


$client = new HttpClient('localhost', 8080);
$client->setDebug(false);

//Authenticate in Geoserver
$client->post('/geoserver/admin/loginSubmit.do', array (
	'username' =>"admin";,
	'password' =>"geoserver",
	'submit' => 'Submit'
));

$headers = $client->getHeaders();
//Sorry, not very nice but I did not manage to make it work other way...
$foo= explode(";",$headers['set-cookie']);
$foo= explode("=",$foo[0]);
$sessionArray = array('JSESSIONID' => $foo[1]);
$client->setCookies($sessionArray);

//Create a new Feature Type
$client->post('/geoserver/config/data/typeNewSubmit.do', array ('selectedNewFeatureType' => $datastore.':::'.$layerName));

//edit feature details
$client->post('/geov2/config/data/typeEditorSubmit.do', array (
	'styleId' => 'poi',
	'SRS' => '4326',
	'title' => 'temporary dataset:' . $layerName,
	'minX' => '-180',
	'minY' => '-90',
	'maxX' => '180',
	'maxY' => '90',
	'keywords' => ' ',
	'abstract' => 'temporary dataset:' . $layerName,
	'schemaBase' => '--',
	'action' => 'Submit'
));

//Click Apply
$client->post('/geov2/admin/saveToGeoServer.do', array ('submit' => 'Apply'));

//Click Save
$client->post('/geov2/admin/saveToXML.do', array ('submit' => 'Save'));



?>

As of Geoserver 1.5.1 we have the ability to compute automatically bbox and SRS, in order to enable it the above code will have to set the autoGenerateExtent flag.
The SRS can still be specified explicitly, if provided, it will be used, otherwise it will be looked up automatically (warning, the lookup may fail, and this will make the configuration fail too).

//edit feature details
$client->post('/geoserver/config/data/typeEditorSubmit.do', array (
	'styleId' => 'poi',
	'title' => 'temporary dataset:' . $layerName,
	'keywords' => ' ',
	'abstract' => 'temporary dataset:' . $layerName,
	'schemaBase' => '--',
	'action' => 'Submit',
	'autoGenerateExtent' => 'true'
));

Applying this patch will allow the creation of new Feature Types with auto-generated SRS And Bounding Box values.

http://jira.codehaus.org/browse/GEOS-1093?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

The above register_featureType.php code could be modified to force the autogeneration by changing the feature details post to:

 $client->post('/geov2/config/data/typeEditorSubmit.do', array (
	'styleId' => 'poi',
	'SRS' => '4326',
	'title' => 'temporary dataset:' . $layerName,
	'minX' => '-180',
	'minY' => '-90',
	'maxX' => '180',
	'maxY' => '90',
	'keywords' => ' ',
	'abstract' => 'temporary dataset:' . $layerName,
	'schemaBase' => '--',	'autoGenerateExtent' => 'true',
	'action' => 'Submit'
));
Posted by leemobile at May 17, 2007 10:30
Document generated by Confluence on Jan 16, 2008 23:26